package LeetCode.LC43;

import java.util.Arrays;

/**
 * @author corn
 * @create 2020-11-08-9:25
 */
public class Solution1 {
    public String multiply(String num1, String num2) {
        if(num1.length()==0 ||num2.length()==0){
            return "0";
        }
        int len1 = num1.length();
        int len2 = num2.length();
        char[] arr1 = num1.toCharArray();
        char[] arr2 = num2.toCharArray();
        int[]  arr = new int[len1 + len2];
        for(int i = len1 -1; i >= 0; i--){
            for(int j = len2 -1; j >= 0; j--){
                //总数
                int i1 =(arr1[i] - '0') * (arr2[j] - '0');
                //个位数的和
                int i2 = arr[j + i + 1] + i1 % 10;
                arr[j+i+1] = i2 % 10;
                arr[i+j] = arr[i+j] + i1/10 + i2/10;
            }
        }

        StringBuilder bu = new StringBuilder();
        boolean flag = true;
        for (int i : arr) {
            if (i == 0 && flag){
                continue;
            }
            flag = false;
            bu.append(i);
        }
        return bu.length() == 0 ? "0" : bu.toString();
    }
}
